home *** CD-ROM | disk | FTP | other *** search
- /*--------------------------- fft2.c ---------------------------------- */
- /* */
- /* Author: Eyal Lebedinsky */
- /* Date: May 1990 */
- /* Version: 25 August 1991 */
- /* */
- /* Example for using the output of fftg.c */
- /* To use this program you need to provide at least: */
- /* move(x,y) */
- /* draw(x,y,WHITE) */
- /* Where x is 0-535 and y is 0-345 (or scale the constants...) */
- /* lines markes as *video* are graphics related. */
- /* */
- /* Warning: */
- /* This program uses 'scanf' et. al. which may not work properly */
- /* in some environments! */
- /* */
- /* This program is released into the public domain. */
- /* */
- /*----------------------------------------------------------------------*/
-
- #include <stdio.h>
-
- #define SC15 32768
-
- #include <graph.h> /*video*/
- #define move(x,y) _moveto (x, y) /*video*/
- #define draw(x,y,c) _lineto (x, y) /*video*/
- #define clear_screen _clearscreen (_GCLEARSCREEN); /*video*/
- static struct videoconfig vc; /*video*/
-
- #define M 8
- #define N (1 << M)
-
- short x[N] = {0}; /* fp(16,0) */
- short qf[(N/2)+1] = {0}; /* fp(16,0) */
- extern void FAR fft ();
-
- static long in[N]; /* fp(32,0) */
-
- main (argc)
- int argc;
- {
- int i, j, k, m, n;
- short dd, dh, dl, y, t ,o; /* fp(16,0) */
- long bf, qq, fff, yl, mf; /* fp(32,0) */
- int tmp;
- char ns[30], fname[35];
- FILE *fin;
-
- /* initialise */
-
- m = M;
- n = 1 << m;
-
- if (_setvideomode (_HERCMONO) == 0) { /*video*/
- printf ("cannot set HERCMONO mode.\n"); /*video*/
- exit (4); /*video*/
- } /*video*/
- _getvideoconfig (&vc); /*video*/
-
- /* end initialise */
-
- again:
-
- printf ("\n\n\nWHICH DATA FILE TO BE USED ");
- scanf (" %s", ns);
- if (strcmp (ns, "end") == 0) {
- _setvideomode (_DEFAULTMODE); /*video*/
- exit (0);
- }
- if (*ns == '\0')
- strcpy (ns, "D1");
-
- strcpy (fname, ns);
- strcat (fname, ".fft");
- fin = fopen (fname, "r");
- if (fin == NULL) {
- perror (fname);
- goto again;
- }
-
- fscanf (fin, " %u", &i); /* ignore count */
-
- fscanf (fin, " %d", &tmp);
- mf = dh = dl = in[0] = dd = tmp;
-
- for (i = 1; i < n; ++i) {
- fscanf (fin, " %d", &tmp);
- mf += in[i] = dd = tmp;
- if (dd > dh) dh = dd;
- if (dd < dl) dl = dd;
- }
- fclose (fin);
-
- y = mf / n;
-
- qq = (long)dh - (long)dl;
- if (qq != 0) { /* scale to maximum */
- for (i = 0; qq < SC15; ++i, qq <<= 1);
- }
- else
- i = 0;
-
- for (j = 0; j < n; ++j)
- x[j] = (in[j] - y) << i;
-
- /* end preprocessing */
-
- clear_screen;
-
- move (15, 40*2);
- draw (15+512, 40*2, WHITE);
- draw (15+512, 1, WHITE);
- draw (15, 1, WHITE);
- draw (15, 40*2, WHITE);
-
- move (15, 40);
- for (i = 0; i < n; ++i)
- draw (16 + i + i, 40 - (x[i] >> 10), WHITE);
-
- move (15, 330); /* X axis */
- draw (15+13*40, 330, WHITE);
- for (i = 0; i <= 130*4; i += 4) {
- move (15+i, 330);
- if (i%(50*4) == 0) /* every 50 */
- draw (15+i, 330+15, WHITE);
- else if (i%(10*4) == 0) /* every 10 */
- draw (15+i, 330+10, WHITE);
- else if (i%(5*4) == 0) /* every 5 */
- draw (15+i, 330+5, WHITE);
- else /* every 1 */
- draw (15+i, 330+2, WHITE);
- }
-
- move (15, 330); /* Y axis */
- draw (15, 330-220, WHITE);
- for (i = 0; i <= 220; i += 220/10) {
- move (15, 330-i);
- draw ((i%5) ? 15-5 : 15-10, 330-i, WHITE);
- }
-
- /* end screen layout */
-
- if (argc > 1) {
- short xx[256];
- long lapse;
-
- memcpy (xx, x, sizeof (xx));
- lapse = time (NULL);
- for (i = 0; i < 10000; ++i) {
- memcpy (x, xx, sizeof (x));
- if (argc < 3)
- fft ();
- }
- lapse = time (NULL) - lapse;
- printf ("time: %lu\n", lapse);
- memcpy (x, xx, sizeof (x));
- }
-
- fft ();
-
- /*
- for (i = 0; i <= n/2; ++i) {
- printf ("%3u= %5d ", i, qf[i]);
- if (!(i % 5))
- printf ("\n");
- }
- printf ("\n");
- */
-
- t = 0;
- for (i = 1; i < n; ++i) {
- if (t < abs (x[i]))
- t = abs (x[i]);
- }
- printf ("mx=%d\n", t);
-
- k = 0;
- t = 0;
- for (i = 1; i <= n/2; ++i) {
- if (t < qf[i])
- t = qf[i];
- qf[i] = isqrt ((long)qf[i]);
- if (k < qf[i])
- k = qf[i];
- }
- printf ("mq=%d\n", t);
-
- if (k == 0)
- k = 1;
-
- t = 15; o = 1 << (10 - m);
- for (i = 1; i <= (n/2); ++i) {
- j = (qf[i] * 220L) / k;
- t = t + o;
- move (t, 330);
- draw (t, 330 - j, WHITE);
- }
-
- goto again;
- }
-
-